package com.mai.cart;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.mai.entity.Product;

public class Cart {
	private Map<Integer, CartItem> map = new HashMap<Integer, CartItem>();
	private double price; //总价
	
	public void add(Product p) {
		CartItem item = map.get(p.getProduct_id());
		if(item == null) {
			item = new CartItem();
			item.setProduct(p);
			item.setQuantity(1);
			map.put(p.getProduct_id(), item);
		}else {
			item.setQuantity(item.getQuantity()+1);
		}
	}
	
	public double getPrice() {
		double counPrice = 0;
		for(Entry<Integer, CartItem> me : map.entrySet()) {
			CartItem item = me.getValue();
			counPrice += item.getPrice();
			this.price = counPrice;
		}
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	
	public Map<Integer, CartItem> getMap() {
		return map;
	}

	public void setMap(Map<Integer, CartItem> map) {
		this.map = map;
	}
	
	
}
